﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string sql1 = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (!IsPostBack)
        {
            Label3.Text = GridView1.Rows[0].Cells[1].Text;
            Label5.Text = GridView1.Rows[0].Cells[1].Text;

            //SqlDataSource_leibie1.SelectCommand = "SELECT * FROM [cailiaoleibie1] WHERE leibie1 in ("+sql1+") ORDER BY [px]";
            Label1.Text = Session["pngeren"].ToString();
            Label2.Text = Session["pn"].ToString();
            if (GridView1.Rows.Count<1)
            {
                string jinggao3 = "<SCRIPT language=javascript>alert('";
                jinggao3 += "参数错误！";
                jinggao3 += "');</SCRIPT>";
                ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
                ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='default.aspx';</SCRIPT>");
            }
        }
        if (GridView1.Rows[0].Cells[1].Text == "接触网")
        {
            //sql1 = "'接触网配件','供电电力金具','电气产品及绝缘子','线缆及附件','标志标牌'";
            sql1 = "'接触网配件'";
        }
        else if (GridView1.Rows[0].Cells[1].Text == "变电")
        {
            sql1 = "'供电电力金具','机电电工产品','电气产品及绝缘子','线缆及附件','标志标牌'";
        }
        else if (GridView1.Rows[0].Cells[1].Text == "电力配电")
        {
            sql1 = "'供电电力金具','机电电工产品','电气产品及绝缘子','线缆及附件','标志标牌'";
        }
        else if (GridView1.Rows[0].Cells[1].Text == "工具配件")
        {
            sql1 = "'工具','工具配件'";
        }
        else if (GridView1.Rows[0].Cells[1].Text == "劳保用品")
        {
            sql1 = "'劳保用品'";
        }
        else if (GridView1.Rows[0].Cells[1].Text == "汽轨车辆")
        {
            sql1 = "'化工油脂','汽轨配件'";
        }
        else if (GridView1.Rows[0].Cells[1].Text == "通用物资")
        {
            sql1 = "'机电电工产品','化工油脂','标准紧固件','钢材','水暖五金器材','其它','杂备品'";
        }
        Label6.Text = sql1.Replace(",'"," ").Replace("'","");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        if (TextBox_chaxun.Text != "")
        {
            SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (leibie1 in (" + sql1 + ") AND [mingcheng] LIKE '%" + TextBox_chaxun.Text + "%') ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (([leibie1] = '" + DropDownList_leibie1.SelectedValue + "') AND ([mingcheng] LIKE '%" + TextBox_chaxun.Text + "%')) ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (([leibie1] = @leibie1) AND ([leibie2] = @leibie2) AND ([mingcheng] LIKE '%' + @mingcheng + '%')) ORDER BY [xuhao]";
        }
        else
        {
            SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE leibie1 in (" + sql1 + ") ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE ([leibie1] = '" + DropDownList_leibie1.SelectedValue + "') ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (([leibie1] = @leibie1) AND ([leibie2] = @leibie2)) ORDER BY [xuhao]";
        }

        //Response.Write(SqlDataSource_cailiaobiao.SelectCommand.ToString());
        //Response.End();
        GridView2.DataBind();
        //Response.Write(GridView2.Rows.Count.ToString());
        if (GridView2.Rows.Count < 1)
        {
            Button2.Visible = false;
        }
        else
        {
            Button2.Visible = true;
        }
    }
    //protected void Button1_Click1(object sender, EventArgs e)
    //{
    //    Button bt=(Button)sender;
    //    GridViewRow grv=(GridViewRow)bt.Parent.Parent;
    //    TextBox textbox1=(TextBox)grv.FindControl("TextBox1");
    //    string txt1=textbox1.Text;
    //    TextBox textbox2=(TextBox)grv.FindControl("TextBox2");
    //    string txt2=textbox2.Text;
    //    try
    //    {
    //        string sqlstr = "INSERT INTO cailiaomingxi (c_id, leibie1, leibie2, bianhao, mingcheng, guige, danwei, danjia, banzushuliang, chejianshuliang, duanshuliang, beizhu) VALUES (";
    //        sqlstr += GridView1.Rows[0].Cells[0].Text+",";
    //        sqlstr += "'"+DropDownList_leibie1.SelectedValue+"',";
    //        sqlstr += "'"+DropDownList_leibie2.SelectedValue+"',";
    //        sqlstr += "'"+grv.Cells[8].Text+"',";
    //        sqlstr += "'"+grv.Cells[9].Text+"',";
    //        sqlstr += "'"+grv.Cells[10].Text+"',";
    //        sqlstr += "'"+grv.Cells[12].Text+"',";
    //        sqlstr += "'"+grv.Cells[13].Text+"',";
    //        sqlstr += txt1+",0,0,"+txt2+")";
    //        //Response.Write(sqlstr);
    //        //Response.End();
    //        sqlcon = new SqlConnection(strCon);
    //        sqlcom = new SqlCommand(sqlstr, sqlcon);
    //        sqlcon.Open();
    //        sqlcom.ExecuteNonQuery();
    //        sqlcon.Close();   
            
    //    }
    //    catch (SqlException)
    //    {
    //        throw;
    //    }

    //    string jinggao3 = "<SCRIPT language=javascript>alert('";
    //    jinggao3 += "添加材料操作成功！";
    //    jinggao3 += "');</SCRIPT>";
    //    ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
    //    //ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='cj_shenpilist.aspx';</SCRIPT>");

    //}
    protected void Button2_Click(object sender, EventArgs e)
    {
        int jiancejishu = 0;
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)GridView2.Rows[i].Cells[1].FindControl("CheckBox1");
            if (chk.Checked == true)
            {
                string txt1 = ((TextBox)GridView2.Rows[i].Cells[1].FindControl("TextBox1")).Text;
                string txt2 = ((TextBox)GridView2.Rows[i].Cells[2].FindControl("TextBox2")).Text;
                if (txt1.Length == 0)
                {
                    string jinggao1 = "alert('";
                    jinggao1 += "请输入序号为 " + (GridView2.PageSize * GridView2.PageIndex+i + 1).ToString() + " 的所选材料数量！";
                    jinggao1 += "');";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao1, true);
                    return;
                }
                for (int j = 0; j < txt1.Trim().Length; j++)
                {
                    char a;
                    int k;
                    a = Convert.ToChar(txt1.Substring(j, 1));
                    k = (int)a;
                    if (k < 48 || k > 57)
                    {
                        string jinggao2 = "alert('";
                        jinggao2 += "请在序号为 " + (GridView2.PageSize * GridView2.PageIndex+i + 1).ToString() + " 材料数量中输入数字！";
                        jinggao2 += "');";
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao2, true);

                        return;
                    }
                }
                jiancejishu++;
            }
        }
        if (jiancejishu == 0)
        {
            string jinggao4 = "alert('";
            jinggao4 += "请至少选择一种材料进行添加！";
            jinggao4 += "');";
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao4, true);
        }
        else
        {
            try
            {
                int jishu = 0;
                //更新明细表
                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    CheckBox chk = (CheckBox)GridView2.Rows[i].Cells[1].FindControl("CheckBox1");
                    if (chk.Checked == true)
                    {
                        string txt1 = ((TextBox)GridView2.Rows[i].Cells[1].FindControl("TextBox1")).Text;
                        string txt2 = ((TextBox)GridView2.Rows[i].Cells[2].FindControl("TextBox2")).Text.Replace("×", "*");
                        string sqlstr = "INSERT INTO cailiaomingxi (c_id,j_id,leibie1,bianhao, mingcheng, guige, danwei, danjia, banzushuliang, chejianshuliang, duanshuliang, daohuoshuliang, beizhu) VALUES (";
                        sqlstr += GridView1.Rows[0].Cells[0].Text + ",";
                        sqlstr += "'" + (GridView2.Rows[i].Cells[15].Text) + "',";
                        //sqlstr += "'" + DropDownList_leibie1.SelectedValue + "',";
                        sqlstr += "'" + (GridView2.Rows[i].Cells[6].Text) + "',";
                        //sqlstr += "'" + Server.HtmlEncode(GridView2.Rows[i].Cells[8].Text).Replace("&amp;nbsp;","") + "',";
                        //sqlstr += "'" + Server.HtmlEncode(GridView2.Rows[i].Cells[9].Text) + "',";
                        //sqlstr += "'" + Server.HtmlEncode(GridView2.Rows[i].Cells[10].Text).Replace("&amp;nbsp;", "") + "',";
                        //sqlstr += "'" + Server.HtmlEncode(GridView2.Rows[i].Cells[12].Text) + "',";
                        sqlstr += "'" + (GridView2.Rows[i].Cells[8].Text).Replace("&amp;nbsp;", "") + "',";
                        sqlstr += "'" + (GridView2.Rows[i].Cells[9].Text) + "',";
                        sqlstr += "'" + (GridView2.Rows[i].Cells[10].Text).Replace("&amp;nbsp;", "") + "',";
                        sqlstr += "'" + (GridView2.Rows[i].Cells[12].Text) + "',";
                        //单价
                        //sqlstr += "'0',";
                        sqlstr += "'" + Server.HtmlEncode(GridView2.Rows[i].Cells[13].Text) + "',";
                        sqlstr += txt1 + ",0,0,0,'" + Server.HtmlEncode(txt2) + "')";
                        //Response.Write(sqlstr);
                        //Response.End();
                        sqlcon = new SqlConnection(strCon);
                        sqlcom = new SqlCommand(sqlstr, sqlcon);
                        sqlcon.Open();
                        sqlcom.ExecuteNonQuery();
                        sqlcon.Close();
                        jishu++;
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }

            string jinggao3 = "alert('";
            jinggao3 += "添加材料操作成功！";
            jinggao3 += "');";
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao3, true);
            //ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='cj_shenpilist.aspx';</SCRIPT>");
            GridView3.DataBind();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (GridView3.Rows.Count < 1)
        {
            string jinggao4 = "alert('";
            jinggao4 += "操作失败！请至少添加一种材料进行提报！";
            jinggao4 += "');";
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao4, true);
            return;
        }
        try
        {
            string sqlstr2 = "update cailiaoguanli set nian='"+System.DateTime.Now.Year.ToString()+"',yue='"+System.DateTime.Now.Month.ToString()+"', addname='" + Label1.Text + "',addtime='" + DateTime.Now.ToString() + "',addip='" + Page.Request.UserHostAddress.ToString().Trim() + "',";
            sqlstr2 += "zt=";            
            if ( Label2.Text.Contains("技术科") == true)
            {
                sqlstr2 += "2";
            }
            //else if (Label2.Text.Contains("车间") == true)
            //{
            //    sqlstr2 += "2";
            //}
            else
            {
                sqlstr2 += "1";
            }
            sqlstr2 += " where c_id=" + Request.QueryString["id"];
            //Response.Write(sqlstr2);
            //Response.End();
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr2, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
        }
        catch (SqlException)
        {
            
            throw;
        }
        string jinggao3 = "alert('";
        jinggao3 += "提报材料计划单操作成功！";
        jinggao3 += "');";
        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao3, true);

        ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='bz_jihuachakan.aspx';</SCRIPT>");

    }
    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?')");
            }
        }
        //计算合计
        double hejib = 0;
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            for (int j = 0; j <= GridView3.Rows.Count - 1; j++)
            {
                GridViewRow row = this.GridView3.Rows[j];
                hejib += Convert.ToInt32(row.Cells[11].Text.ToString()) * Convert.ToDouble(row.Cells[10].Text.ToString());
            }
            e.Row.Cells[10].Text = "总计";
            e.Row.Cells[11].Text = "班组" + hejib.ToString() + "元";
            e.Row.Cells[11].ForeColor = System.Drawing.Color.Red;
            //Button3.OnClientClick = "return confirm('此次提报总计" + hejib.ToString() + "元！计划单提报后将无法修改，确认继续吗？')";
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
            e.Row.Cells[6].Text = Server.HtmlDecode(e.Row.Cells[6].Text);
            e.Row.Cells[8].Text = e.Row.Cells[8].Text.Replace("&amp;#215;", "×").Replace("&amp;quot;", "");
            e.Row.Cells[10].Text = Server.HtmlDecode(e.Row.Cells[10].Text);
        }
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
        if (e.Row.RowIndex != -1)
        {
            int id = GridView2.PageSize*GridView2.PageIndex+ e.Row.RowIndex + 1;
            e.Row.Cells[3].Text = id.ToString();
        }

    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //if (GridView1.Rows[0].Cells[1].Text == "接触网")
        //{
        //    sql1 = "'接触网配件','供电电力金具','电气产品及绝缘子','线缆及附件','标志标牌'";
        //}
        //else if (GridView1.Rows[0].Cells[1].Text == "变电")
        //{
        //    sql1 = "'供电电力金具','机电电工产品','电气产品及绝缘子','线缆及附件','标志标牌'";
        //}
        //else if (GridView1.Rows[0].Cells[1].Text == "电力配电")
        //{
        //    sql1 = "'供电电力金具','机电电工产品','电气产品及绝缘子','线缆及附件','标志标牌'";
        //}
        //else if (GridView1.Rows[0].Cells[1].Text == "工具配件")
        //{
        //    sql1 = "'工具','工具配件'";
        //}
        //else if (GridView1.Rows[0].Cells[1].Text == "劳保用品")
        //{
        //    sql1 = "'劳保用品'";
        //}
        //else if (GridView1.Rows[0].Cells[1].Text == "汽轨车辆")
        //{
        //    sql1 = "'化工油脂','汽轨配件'";
        //}
        //else if (GridView1.Rows[0].Cells[1].Text == "通用物资")
        //{
        //    sql1 = "'机电电工产品','化工油脂','标准紧固件','钢材','水暖五金器材','其它','杂备品'";
        //}
        if (TextBox_chaxun.Text != "")
        {
            SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (leibie1 in (" + sql1 + ") AND [mingcheng] LIKE '%" + TextBox_chaxun.Text + "%') ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (([leibie1] = '" + DropDownList_leibie1.SelectedValue + "') AND ([mingcheng] LIKE '%" + TextBox_chaxun.Text + "%')) ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (([leibie1] = @leibie1) AND ([leibie2] = @leibie2) AND ([mingcheng] LIKE '%' + @mingcheng + '%')) ORDER BY [xuhao]";
        }
        else
        {
            SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE leibie1 in (" + sql1 + ") ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE ([leibie1] = '" + DropDownList_leibie1.SelectedValue + "') ORDER BY [xuhao]";
            //SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (([leibie1] = @leibie1) AND ([leibie2] = @leibie2)) ORDER BY [xuhao]";
        }

        //Response.Write(SqlDataSource_cailiaobiao.SelectCommand.ToString());
        GridView2.DataBind();
        //Response.Write(GridView2.Rows.Count.ToString());
    }
    protected void GridView2_Sorted(object sender, EventArgs e)
    {
        if (TextBox_chaxun.Text != "")
        {
            SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE (leibie1 in (" + sql1 + ") AND [mingcheng] LIKE '%" + TextBox_chaxun.Text + "%') ORDER BY [xuhao]";
        }
        else
        {
            SqlDataSource_cailiaobiao.SelectCommand = "SELECT * FROM [cailiaojichuku] WHERE leibie1 in (" + sql1 + ") ORDER BY [xuhao]";
        }

        GridView2.DataBind();

    }
}